package com.flaremars.picker.activities;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.flaremars.picker.R;
import com.flaremars.picker.adapters.common.BaseRecyclerAdapter;
import com.flaremars.picker.constants.ProjectConstants;
import com.flaremars.picker.entities.common.BaseEntity;
import com.flaremars.picker.fragments.common.BaseListFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ForTestingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_testing);

        getSupportFragmentManager().beginTransaction().replace(R.id.container,new ForTestingFragment()).commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
//            startActivity(new Intent(this, MainActivity.class));
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private static class ForTestingObject extends BaseEntity {
        private String something;
    }

    public static class ForTestingFragment extends BaseListFragment<ForTestingObject> {

        @Override
        protected RecyclerView.Adapter setupAdapter(List<ForTestingObject> dataObjects) {
            return new ForTestingAdapter2(getContext(),getContentRecyclerView(),dataObjects);
        }

        @Override
        protected String setupDataRequestUrl() {
            return ProjectConstants.BASE_URL + "test/getSomething";
        }

        @Override
        protected List<ForTestingObject> parseJson(String jsonStr) {
            List<ForTestingObject> list = new ArrayList<>();
            Log.e("tag", jsonStr);
            try {
                JSONObject rootJson = new JSONObject(jsonStr);
                Integer statusCode = rootJson.getInt("statusCode");
                String msg = rootJson.getString("message");
                if (statusCode.equals(ProjectConstants.STATUS_CODE_ERROR)) {
                    changeFragmentStatus(FragmentStatus.REQUEST_ERROR);
                    return list;
                } else if (statusCode.equals(ProjectConstants.STATUS_CODE_NO_MORE_DATA)) {
                    changeFragmentStatus(FragmentStatus.NO_DATA);
                    return list;
                }
                JSONObject data = rootJson.getJSONObject("data");

                int curPage = data.getInt("curPage");
                int totalPage = data.getInt("totalPage");
                updatePageNum(curPage,totalPage);

                JSONArray objectsJson = data.getJSONArray("objects");
                for (int i = 0;i < objectsJson.length();i++) {
                    JSONObject temp = objectsJson.getJSONObject(i);
                    ForTestingObject tempEntity = new ForTestingObject();
                    tempEntity.something = temp.getString("something");

                    list.add(tempEntity);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return list;
        }

        @Override
        protected Map<String, String> setupUrlParams() {
            Map<String,String> paramsMap = new HashMap<>();
            paramsMap.put("curPage",String.valueOf(getCurPage()));
            return paramsMap;
        }

        private static class ForTestingAdapter2 extends BaseRecyclerAdapter<ForTestingObject> {

            public ForTestingAdapter2() {

            }

            public ForTestingAdapter2(final Context context,ViewGroup parentView,List<ForTestingObject> objects) {
                setDatas(objects);
                View headerView = LayoutInflater.from(context).inflate(R.layout.view_bbs_fragment_header,parentView,false);
                setHeaderView(headerView);
                setOnItemClickListener(new OnItemClickListener<ForTestingObject>() {
                    @Override
                    public void onItemClick(int position, ForTestingObject data) {
                        Toast.makeText(context,data.something,Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
                View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_test, parent, false);
                return new MyHolder(layout);
            }

            @Override
            public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, ForTestingObject data) {
                if(viewHolder instanceof MyHolder) {
                    ((MyHolder) viewHolder).somethingTextView.setText(data.something);
                }
            }

            class MyHolder extends BaseRecyclerAdapter.Holder {
                private TextView somethingTextView;

                public MyHolder(View itemView) {
                    super(itemView);
                    somethingTextView = (TextView) itemView.findViewById(R.id.tv_test_content);
                }
            }
        }
    }

    private static class ForTestingAdapter extends RecyclerView.Adapter<ForTestingAdapter.ViewHolder> {

        private static final String TAG = "ForTestingAdapter";

        private List<ForTestingObject> testingObjects;

        private Context context;

        public ForTestingAdapter(Context context, List<ForTestingObject> testingObjects) {
            this.context = context;
            this.testingObjects = testingObjects;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private TextView somethingTextView;

            public ViewHolder(View itemView) {
                super(itemView);
                somethingTextView = (TextView) itemView.findViewById(R.id.tv_test_content);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context)
                    .inflate(R.layout.item_view_test, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            final ForTestingObject item = testingObjects.get(position);
            holder.somethingTextView.setText(item.something);
        }

        @Override
        public int getItemCount() {
            return testingObjects.size();
        }
    }


}
